package com.cicigodd.brick.plug.system.dict.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cicigodd.brick.plug.common.exception.BusinessException;
import com.cicigodd.brick.plug.common.factory.PageFactory;
import com.cicigodd.brick.plug.common.factory.PageResultFactory;
import com.cicigodd.brick.plug.common.response.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cicigodd.brick.plug.system.dict.entity.SysDictValue;
import com.cicigodd.brick.plug.system.dict.mapper.SysDictValueMapper;
import com.cicigodd.brick.plug.system.dict.request.*;
import com.cicigodd.brick.plug.system.dict.response.SysDictValueDetailResponse;
import com.cicigodd.brick.plug.system.dict.response.SysDictValueListResponse;
import com.cicigodd.brick.plug.system.dict.service.SysDictValueService;
import com.cicigodd.brick.plug.system.utils.pinyin.api.PinYinApi;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Arrays;

/**
 * 系统字典
 *
 * @author cicigodd
 * @date 2022/12/12 13:27:02
 */
@Service
public class SysDictValueServiceImpl extends ServiceImpl<SysDictValueMapper, SysDictValue> implements SysDictValueService {

    @Resource
    private PinYinApi pinYinApi;

    /**
     * 添加
     *
     * @author cicigodd
     * @date 2022/12/12 13:27:02
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(SysDictValueAddRequest sysDictValueAddRequest) {
        SysDictValue sysDictValue = new SysDictValue();
        BeanUtil.copyProperties(sysDictValueAddRequest, sysDictValue);
        String firstLetterUpper = pinYinApi.parsePinyinString(sysDictValueAddRequest.getDictLabel());
        sysDictValue.setDictCode(sysDictValueAddRequest.getDictType() + ":" + firstLetterUpper);
        this.save(sysDictValue);
    }

    /**
     * 删除
     *
     * @author cicigodd
     * @date 2022/12/12 13:27:02
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(SysDictValueDeleteRequest sysDictValueDeleteRequest) {
        this.removeByIds(sysDictValueDeleteRequest.getIds());
    }


    /**
     * 编辑
     *
     * @author cicigodd
     * @date 2022/12/12 13:27:02
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void edit(SysDictValueEditRequest sysDictValueEditRequest) {
        SysDictValue sysDictValue = this.getById(sysDictValueEditRequest.getId());
        if (ObjectUtil.isEmpty(sysDictValue)) {
            throw new BusinessException("查询结果不存在");
        }
        BeanUtil.copyProperties(sysDictValueEditRequest, sysDictValue);
        this.updateById(sysDictValue);
    }

    /**
     * 详情
     *
     * @author cicigodd
     * @date 2022/12/12 13:27:02
     */
    @Override
    public SysDictValueDetailResponse detail(SysDictValueDetailRequest sysDictValueDetailRequest) {
        SysDictValue sysDictValue = this.getById(sysDictValueDetailRequest.getId());
        if (ObjectUtil.isEmpty(sysDictValue)) {
            throw new BusinessException("查询结果不存在");
        }
        SysDictValueDetailResponse sysDictValueDetailResponse = new SysDictValueDetailResponse();
        BeanUtil.copyProperties(sysDictValue, sysDictValueDetailResponse);
        return sysDictValueDetailResponse;
    }

    /**
     * 列表
     *
     * @author cicigodd
     * @date 2022/12/12 13:27:02
     */
    @Override
    public PageResult<SysDictValueListResponse> list(SysDictValueListRequest sysDictValueListRequest) {
        LambdaQueryWrapper<SysDictValue> wrapper = new LambdaQueryWrapper<>();
        String dictType = sysDictValueListRequest.getDictType();
        String dictCode = sysDictValueListRequest.getDictCode();
        String dictLabel = sysDictValueListRequest.getDictLabel();
        String dictValue = sysDictValueListRequest.getDictValue();
        String listClass = sysDictValueListRequest.getListClass();
        String remark = sysDictValueListRequest.getRemark();
        Integer sort = sysDictValueListRequest.getSort();

        wrapper.like(ObjectUtil.isNotEmpty(dictType), SysDictValue::getDictType, dictType);
        wrapper.like(ObjectUtil.isNotEmpty(dictCode), SysDictValue::getDictCode, dictCode);
        wrapper.like(ObjectUtil.isNotEmpty(dictLabel), SysDictValue::getDictLabel, dictLabel);
        wrapper.like(ObjectUtil.isNotEmpty(dictValue), SysDictValue::getDictValue, dictValue);
        wrapper.like(ObjectUtil.isNotEmpty(listClass), SysDictValue::getListClass, listClass);
        wrapper.like(ObjectUtil.isNotEmpty(remark), SysDictValue::getRemark, remark);
        wrapper.eq(ObjectUtil.isNotNull(sort), SysDictValue::getSort, sort);
        IPage<SysDictValueListResponse> page = this.page(PageFactory.defaultPage(sysDictValueListRequest.getPageNum(), sysDictValueListRequest.getPageSize()), wrapper).convert(SysDictValue -> BeanUtil.copyProperties(SysDictValue, SysDictValueListResponse.class));
        return PageResultFactory.createPageResult(page);
    }

    /**
     * 根据字典类型和编码查询
     * @param dictType
     * @param dictCode
     * @return
     */
    @Override
    public SysDictValueDetailResponse detailByTypeAndCode(String dictType, String dictCode) {
        LambdaQueryWrapper<SysDictValue> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysDictValue::getDictType, dictType);
        wrapper.eq(SysDictValue::getDictCode, dictCode);
        SysDictValue sysDictValue = this.getOne(wrapper);
        return BeanUtil.copyProperties(sysDictValue, SysDictValueDetailResponse.class);
    }

    /**
     * 根据字典类型和字典值查询
     * @param dictType
     * @param value
     * @return
     */
    @Override
    public SysDictValueDetailResponse detailByTypeAndValue(String dictType, String value) {
        LambdaQueryWrapper<SysDictValue> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysDictValue::getDictType, dictType);
        wrapper.eq(SysDictValue::getDictValue, value);
        SysDictValue sysDictValue = this.getOne(wrapper);
        return BeanUtil.copyProperties(sysDictValue, SysDictValueDetailResponse.class);
    }

    /**
     * 根据字典类型和字典值查询字典名称
     * @param dictType
     * @param value
     * @return
     */
    @Override
    public String getTypeAndValue(String dictType, String value) {
        LambdaQueryWrapper<SysDictValue> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysDictValue::getDictType, dictType);
        wrapper.eq(SysDictValue::getDictValue, value);
        SysDictValue sysDictValue = this.getOne(wrapper);
        return sysDictValue.getDictLabel();
    }

    /**
     * 根据字典类型和编码查询值
     * @param dictType
     * @param dictCode
     * @return
     */
    @Override
    public String getByTypeAndCode(String dictType, String dictCode) {
        LambdaQueryWrapper<SysDictValue> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysDictValue::getDictType, dictType);
        wrapper.eq(SysDictValue::getDictCode, dictCode);
        SysDictValue sysDictValue = this.getOne(wrapper);
        return sysDictValue.getDictValue();
    }

    /**
     * 根据字典类型
     *
     * @param
     * @return
     */
    @Override
    public List<SysDictValueDetailResponse> detailByType(String dictType) {
        LambdaQueryWrapper<SysDictValue> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysDictValue::getDictType, dictType);
        List<SysDictValue> list = this.list(wrapper);
        return BeanUtil.copyToList(list, SysDictValueDetailResponse.class);
    }


}
